package jt.example.alertdialog;

import android.animation.AnimatorSet;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by TaoJn on 4/5/2016.
 */
public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.main);
        registerBasicAlertDialog();
        registerAlertDialogWithItems();
        registerMultipleAlertDialog();
        registerSingleAlertDialog();
        registerRichAlertDialog();
    }

    private void registerBasicAlertDialog(){
        Button button = (Button) this.findViewById(R.id.btnSimple);

        button.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.advise);
                builder.setTitle("系统提示");
                builder.setMessage("带取消， 中立， 确定按钮的对话框！");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_LONG);
                        toast.show();
                    }
                });

                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_LONG);
                        toast.show();
                    }
                });

                builder.setNeutralButton("中立", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(MainActivity.this, "中立", Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.BOTTOM, 0, 0);
                        toast.show();
                    }
                });

                builder.show();
            }
        });
    }

    private void registerAlertDialogWithItems() {
        Button button = (Button) this.findViewById(R.id.btnItems);
        button.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String[] items = new String[]{"跑步", "羽毛球", "乒乓球", "网球", "体操"};

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.advise);
                builder.setTitle("请选择你喜欢的体育项目？");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(MainActivity.this, "你选择的项目是：" + items[which], Toast.LENGTH_SHORT);
                        toast.show();
                    }
                });

                builder.create().show();
            }
        });
    }

    private void registerMultipleAlertDialog(){
        Button button = (Button) this.findViewById(R.id.btnMultipleChoice);
        button.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                final String[] items = new String[]{"植物大战僵尸", "愤怒的小鸟", "泡泡龙", "开心农场", "超级玛雅"};
                final boolean[] checkedItems = new boolean[]{true, true, false, false, false};

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.advise);
                builder.setTitle("请选择你喜欢的游戏？");
                builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        checkedItems[which] = isChecked;
                    }
                });

                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        String choicesText = "";

                        for (int i = 0; i < items.length; i++) {
                            if (checkedItems[i]) {
                                choicesText = choicesText + items[i] + " ";
                            }
                        }

                        Toast toast = Toast.makeText(MainActivity.this, "你选择的游戏：" + choicesText, Toast.LENGTH_SHORT);
                        toast.show();
                    }
                });
                builder.create().show();
            }
        });
    }

    private void registerSingleAlertDialog() {
        Button button = (Button) this.findViewById(R.id.btnSingleChoice);
        button.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] items = new String[]{"静音", "标准", "无声", "会议", "户外"};

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.advise);
                builder.setTitle("请选择情景模式？");
                builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(MainActivity.this, "你选择的情景模式：" + items[which], Toast.LENGTH_SHORT);
                        toast.show();
                    }
                });
                builder.create().show();
                builder.setPositiveButton("确定", null);
            }
        });
    }

    private void registerRichAlertDialog() {
        Button button = (Button) this.findViewById(R.id.btnRichChoice);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final int[] choiceIcons = new int[]{
                        R.drawable.icon_home,
                        R.drawable.icon_setting,
                        R.drawable.icon_search,
                        R.drawable.icon_camera,
                        R.drawable.icon_powoff,
                        R.drawable.icon_logon
                };

                final  String[] choiceTexts = getResources().getStringArray(R.array.choices);

                List<Map<String, Object>> choices = new ArrayList<Map<String, Object>>();

                for(int i=0; i<choiceTexts.length; i++){
                    Map<String, Object> choice = new HashMap<String, Object>();
                    choice.put("image", choiceIcons[i]);
                    choice.put("text", choiceTexts[i]);
                    choices.add(choice);
                }

                final SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,
                        choices,
                        R.layout.rich_choice_item,
                        new String[]{"image", "text"},
                        new int[]{R.id.choiceIcon, R.id.choiceText});

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.advise);
                builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(MainActivity.this, "你选择的是：" + choiceTexts[which], Toast.LENGTH_SHORT);
                        toast.show();
                    }
                });

                builder.create().show();
            }
        });
    }
}
